lib/sysroot: Add non-failable ostree_sysroot_repo()
authorColin Walters <walters@verbum.org>
Thu, 25 May 2017 23:38:52 +0000 (19:38 -0400)
committerAtomic Bot <atomic-devel@projectatomic.io>
Fri, 26 May 2017 19:17:59 +0000 (19:17 +0000)
Having a failable accessor is annoying, since it's really common
to reference both.  Instead, open the repo once when we load
the sysroot, and provide a non-failable accessor.

This is also prep for `ostree_repo_open_at()`, which collapses the separation
between `ostree_repo_new()` and `ostree_repo_open()`.

Closes: #886
Approved by: jlebon

apidoc/ostree-sections.txt
src/libostree/libostree.sym
src/libostree/ostree-sysroot-cleanup.c
src/libostree/ostree-sysroot-deploy.c
src/libostree/ostree-sysroot-private.h
src/libostree/ostree-sysroot.c
src/libostree/ostree-sysroot.h

index adc2dfd74078ac7279da571c8be81ad7d2145cd7..56c5c94eb5acd797d42cad26097c6ac620d215e2 100644 (file)
@@ -486,6 +486,7 @@ ostree_sysroot_get_deployment_dirpath
 ostree_sysroot_get_deployment_origin_path
 ostree_sysroot_cleanup
 ostree_sysroot_prepare_cleanup
+ostree_sysroot_repo
 ostree_sysroot_get_repo
 ostree_sysroot_init_osname
 ostree_sysroot_deployment_set_kargs
index e2a7c3d938366bdacdc37828b2bfbfbf4cea3fee..612eb8acdfc59e2b7718f0f7266ec435b8bc3a26 100644 (file)
@@ -402,12 +402,10 @@ global:
  *                         NOTE NOTE NOTE
  */
 
-/*
-LIBOSTREE_2017.$NEWVERSION {
+LIBOSTREE_2017.7 {
 global:
-       someostree_symbol_deleteme;
+  ostree_sysroot_repo;
 } LIBOSTREE_2017.6;
-*/
 
 /* Stub section for the stable release *after* this development one; don't
  * edit this other than to update the last number.  This is just a copy/paste
index 499f20eb855a0b95f7ce74d0dddc288bf4bb0046..c0b8bf0715b4e5bef5582ed13853b8e1be56e165 100644 (file)
@@ -501,8 +501,6 @@ _ostree_sysroot_cleanup_internal (OstreeSysroot              *self,
                                   GCancellable               *cancellable,
                                   GError                    **error)
 {
-  glnx_unref_object OstreeRepo *repo = NULL;
-
   g_return_val_if_fail (OSTREE_IS_SYSROOT (self), FALSE);
   g_return_val_if_fail (self->loaded, FALSE);
 
@@ -512,9 +510,7 @@ _ostree_sysroot_cleanup_internal (OstreeSysroot              *self,
   if (!cleanup_old_deployments (self, cancellable, error))
     return FALSE;
 
-  if (!ostree_sysroot_get_repo (self, &repo, cancellable, error))
-    return FALSE;
-
+  OstreeRepo *repo = ostree_sysroot_repo (self);
   if (!generate_deployment_refs (self, repo,
                                  self->bootversion,
                                  self->subbootversion,
index 6df4fff215fd656b36ba55802ec0cd37b0ceee5b..2b45e6dac62eff1c45d57802f060888ed92fc339 100644 (file)
@@ -1956,10 +1956,7 @@ ostree_sysroot_deploy_tree (OstreeSysroot     *self,
   if (!glnx_opendirat (self->sysroot_fd, osdeploypath, TRUE, &os_deploy_dfd, error))
     return FALSE;
 
-  glnx_unref_object OstreeRepo *repo = NULL;
-  if (!ostree_sysroot_get_repo (self, &repo, cancellable, error))
-    return FALSE;
-
+  OstreeRepo *repo = ostree_sysroot_repo (self);
   glnx_unref_object OstreeDeployment *merge_deployment = NULL;
   if (provided_merge_deployment != NULL)
     merge_deployment = g_object_ref (provided_merge_deployment);
index 26cfd363e126395bdacb01d023fc3ce742f20564..14ee5cad965c4c8730272858083d0fe66b9d85c2 100644 (file)
@@ -57,6 +57,7 @@ struct OstreeSysroot {
 
   /* Only access through ostree_sysroot_get_repo() */
   OstreeRepo *repo;
+  gboolean repo_opened;
 
   OstreeSysrootDebugFlags debug_flags;
 };
index ac24b0afc4d2a1b789094ae564f5c6d8e94f60e6..86aa8ce36a3e610f1e59416891fdd3e397819489 100644 (file)
@@ -729,6 +729,18 @@ ostree_sysroot_load (OstreeSysroot  *self,
   return ostree_sysroot_load_if_changed (self, NULL, cancellable, error);
 }
 
+static gboolean
+ensure_repo_opened (OstreeSysroot  *self,
+                    GError        **error)
+{
+  if (self->repo_opened)
+    return TRUE;
+  if (!ostree_repo_open (self->repo, NULL, error))
+    return FALSE;
+  self->repo_opened = TRUE;
+  return TRUE;
+}
+
 gboolean
 ostree_sysroot_load_if_changed (OstreeSysroot  *self,
                                 gboolean       *out_changed,
@@ -738,6 +750,13 @@ ostree_sysroot_load_if_changed (OstreeSysroot  *self,
   if (!ensure_sysroot_fd (self, error))
     return FALSE;
 
+  /* Here we also lazily initialize the repository.  We didn't do this
+   * previous to v2017.6, but we do now to support the error-free
+   * ostree_sysroot_repo() API.
+   */
+  if (!ensure_repo_opened (self, error))
+    return FALSE;
+
   int bootversion = 0;
   if (!read_current_bootversion (self, &bootversion, cancellable, error))
     return FALSE;
@@ -918,8 +937,7 @@ ostree_sysroot_get_repo (OstreeSysroot         *self,
                          GCancellable  *cancellable,
                          GError       **error)
 {
-  /* ostree_repo_open() is idempotent. */
-  if (!ostree_repo_open (self->repo, cancellable, error))
+  if (!ensure_repo_opened (self, error))
     return FALSE;
 
   if (out_repo != NULL)
@@ -927,6 +945,24 @@ ostree_sysroot_get_repo (OstreeSysroot         *self,
   return TRUE;
 }
 
+/**
+ * ostree_sysroot_repo:
+ * @self: Sysroot
+ *
+ * This function is a variant of ostree_sysroot_get_repo() that cannot fail, and
+ * returns a cached repository. Can only be called after ostree_sysroot_load()
+ * has been invoked successfully.
+ *
+ * Returns: (transfer none): The OSTree repository in sysroot @self.
+ */
+OstreeRepo *
+ostree_sysroot_repo (OstreeSysroot *self)
+{
+  g_return_val_if_fail (self->loaded, NULL);
+  g_assert (self->repo);
+  return self->repo;
+}
+
 /**
  * ostree_sysroot_query_bootloader:
  * @sysroot: Sysroot
index 09614b555261faa83d84a1924956b51837460d27..e5969e9e2795a90e0929cec784e9ce35b7b25bad 100644 (file)
@@ -126,6 +126,9 @@ gboolean ostree_sysroot_write_origin_file (OstreeSysroot         *sysroot,
                                            GCancellable          *cancellable,
                                            GError               **error);
 
+_OSTREE_PUBLIC
+OstreeRepo * ostree_sysroot_repo (OstreeSysroot *self);
+
 _OSTREE_PUBLIC
 gboolean ostree_sysroot_get_repo (OstreeSysroot         *self,
                                   OstreeRepo           **out_repo,